package com.atguigu.tingshu.handler;

import com.atguigu.tingshu.model.CDCEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import javax.print.attribute.standard.PrinterURI;

/**
 * 负责监听tingshu_user库下user_info表信息变更
 *
 * @author: atguigu
 * @create: 2024-05-21 11:48
 */
@Slf4j
@Component
@CanalTable("user_info")
public class UserCdcHandler implements EntryHandler<CDCEntity> {


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insert(CDCEntity cdcEntity) {
        EntryHandler.super.insert(cdcEntity);
    }

    /**
     * 监听到用户信息被修改
     *
     * @param before 变更前数据
     * @param after  变更后数据
     */
    @Override
    public void update(CDCEntity before, CDCEntity after) {
        if (after != null) {
            log.info("[cdc]监听到user_info变更数据：{}", after);
            String key = "user:info:"+after.getId();
            //删除Redis中用户信息
            redisTemplate.delete(key);
        }
    }

    @Override
    public void delete(CDCEntity cdcEntity) {
        if (cdcEntity != null) {
            log.info("[cdc]监听到user_info删除数据：{}", cdcEntity);
            String key = "user:info:"+cdcEntity.getId();
            //删除Redis中用户信息
            redisTemplate.delete(key);
        }
    }
}
